package com.example.oasystem.controller.daymanage;


import com.example.oasystem.dao.daymanage.AoaScheduleListDao;
import com.example.oasystem.dao.process.AoaProcessListDaoOfChen;
import com.example.oasystem.entity.daymanage.AoaScheduleList;
import com.example.oasystem.entity.daymanage.EmpDTO;
import com.example.oasystem.entity.daymanage.mingzi;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.impl.DayManageServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller

public class DayManageController {
    @Autowired
    AoaScheduleListDao scheduleListDao;
    @Autowired
    AoaProcessListDaoOfChen processListDaoOfChen;
    @Autowired
    DayManageServiceImpl dayManageService;
    //初始进入页面
    @RequestMapping("daymanage")
    public String daymanage(@RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                            Model model,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                            HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int i = scheduleListDao.myDay(Math.toIntExact(user.getUserId()));
        if (i == 0) {
            //是否可修改
            model.addAttribute("ismyday", null);
        } else {
            //是否可修改
            model.addAttribute("ismyday", i);
        }
        List<AoaTypeList> processType = scheduleListDao.findProcessType();
        List<AoaStatusList> processStatus = scheduleListDao.findProcessStatus();
        PageInfo<AoaScheduleList> sche = dayManageService.getSche(Math.toIntExact(user.getUserId()), baseKey, pageNum, pageSize);
        List<AoaScheduleList> list = sche.getList();
        Page page=new Page(sche.getSize(), Math.toIntExact(sche.getTotal()),sche.getPages(),sche.getPageNum(),sche.isIsFirstPage(),sche.isIsLastPage());
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", page);
        model.addAttribute("schedules", list);
        model.addAttribute("url", "daypage");
        //类型
        model.addAttribute("types", processType);
        //状态
        model.addAttribute("statuses", processStatus);
        //当前页数
        model.addAttribute("pageNum",pageNum);
        /*List<AoaScheduleList> sche = scheduleListDao.getSche(user.getUserId(), text);//查询内容*/
        return "daymanage/daymanage";
    }
    @RequestMapping("daypage")
    public String daypage(@RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                            Model model,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                            HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        int i = scheduleListDao.myDay(Math.toIntExact(user.getUserId()));
        if (i == 0) {
            //是否可修改
            model.addAttribute("ismyday", null);
        } else {
            //是否可修改
            model.addAttribute("ismyday", i);
        }
        List<AoaTypeList> processType = scheduleListDao.findProcessType();
        List<AoaStatusList> processStatus = scheduleListDao.findProcessStatus();
        PageInfo<AoaScheduleList> sche = dayManageService.getSche(Math.toIntExact(user.getUserId()), baseKey, pageNum, pageSize);
        List<AoaScheduleList> list = sche.getList();
        Page page=new Page(sche.getSize(), Math.toIntExact(sche.getTotal()),sche.getPages(),sche.getPageNum(),sche.isIsFirstPage(),sche.isIsLastPage());
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", page);
        model.addAttribute("schedules", list);
        model.addAttribute("url", "daypage");
        //类型
        model.addAttribute("types", processType);
        //状态
        model.addAttribute("statuses", processStatus);
        //当前页数
        model.addAttribute("pageNum",pageNum);
        /*List<AoaScheduleList> sche = scheduleListDao.getSche(user.getUserId(), text);//查询内容*/
        return "daymanage/daymanagepaging";
    }
    //进入新增或修改页面
    @RequestMapping("/dayedit")
    public String dayedit(@RequestParam(value = "rcid", defaultValue = "0") int rcid, Model model,
                          @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                          HttpSession session) {

        List<mingzi> shar = scheduleListDao.shar(rcid);

        int size = shar.size();
        System.out.println("size"+size);
        String names =" ";
        for(int i=0;i<shar.size();i++){
            names+=shar.get(i).getUsers();
        }
        model.addAttribute("names",names);
        model.addAttribute("rcid", rcid);
        AoaUser user = (AoaUser) session.getAttribute("user");
        AoaScheduleList uper = scheduleListDao.getUper(rcid);
        List<AoaTypeList> type = scheduleListDao.getType();
        List<AoaStatusList> statu = scheduleListDao.getStatu();
        List<EmpDTO> lowEmps = scheduleListDao.getLowEmps(Math.toIntExact(user.getUserId()));
        System.out.println("uper" + uper);
        model.addAttribute("rc", uper);
        model.addAttribute("types", type);
        model.addAttribute("statuses", statu);
        model.addAttribute("topemplist", lowEmps);
        model.addAttribute("pageNum",pageNum);
        return "daymanage/editday";
    }

    //新增和修改
    @RequestMapping("addandchangeday")
    public String addandchangeday(Model model,
                                  HttpSession session,
                                  @RequestParam(value = "rcId", defaultValue = "0") int rcId,
                                  @RequestParam(value = "startTime", defaultValue = "") String startTime,
                                  @RequestParam(value = "endTime", defaultValue = "") String endTime,
                                  @RequestParam(value = "title", defaultValue = "") String title,
                                  @RequestParam(value = "shareuser", defaultValue = "") String shareuser,
                                  @RequestParam(value = "typeId", defaultValue = "") int typeId,
                                  @RequestParam(value = "statusId", defaultValue = "") int statusId,
                                  @RequestParam(value = "isRemind", defaultValue = "") String isRemind,
                                  @RequestParam(value = "describe", defaultValue = "") String describe,
                                  @RequestParam(value = "pageNum",defaultValue = "0")int pageNum) {
        System.out.println("这是描述"+describe);


        //获取当前系统时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String now = formatter.format(date);
        //获取当前登录信息
        AoaUser use = (AoaUser) session.getAttribute("user");
        int jizhu=0;
        System.out.println(isRemind);
        if (isRemind.equals("on"))
        {
            jizhu=1;
        }
        if (rcId != 0) {
            System.out.println("这是修改");
            scheduleListDao.setShe(rcId, typeId, statusId, startTime, endTime, title, describe,jizhu);

            //进入修改方法
            if (!shareuser.trim().equals("")) {
                String[] split = shareuser.split(";");
                List<AoaUser> user = scheduleListDao.getUserId(split);
                List<Integer> userid = new ArrayList();
                for (int i = 0; i < user.size(); i++) {
                    userid.add(Math.toIntExact(user.get(i).getUserId()));
                }
                //开始添加附表数据
                dayManageService.delUser(rcId, userid);
            }
        } else {
            //进入增添方法
            System.out.println("这是新增");
            System.out.println("shareuser = " + shareuser);
            int id = dayManageService.InsertShe(new AoaScheduleList(now, endTime, startTime, statusId, title, typeId, Math.toIntExact(use.getUserId()), describe,jizhu,0));
            System.out.println("新的id值为" + id);
            if (!shareuser.trim().equals("")) {

                String[] split = shareuser.split(";");
                List<AoaUser> user = scheduleListDao.getUserId(split);
                List<Integer> userid = new ArrayList();
                for (int i = 0; i < user.size(); i++) {
                    userid.add(Math.toIntExact(user.get(i).getUserId()));
                    System.out.println("id = " + Math.toIntExact(user.get(i).getUserId()));
                }
                System.out.println(userid);
                for (Integer integer : userid) {
                    scheduleListDao.Insertuser2(id, integer);
                }
                //开始增添附表数据
            }
        }
        return "forward:/daymanage?pageNum="+pageNum;


    }
    @RequestMapping("dayremove")
    public String dayremove(@RequestParam(value = "rcid",defaultValue = "0")int rcid,
                            @RequestParam(value = "pageNum",defaultValue = "0")int pageNum){
        dayManageService.delShe(rcid);
        return "forward:/daymanage?pageNum="+pageNum;
    }
}
